package com.versiFramework.services{
    import com.versiFramework.core.IInitializeClass;
    import com.versiFramework.core.IVersiClass;
    import com.versiFramework.events.versiEvent;
    import com.versiFramework.versi;
    
    import flash.events.Event;
	//import mx.events.PropertyChangeEvent;
    import flash.events.EventDispatcher;
    
    import mx.messaging.Channel;
    import mx.messaging.ChannelSet;
    import mx.messaging.channels.AMFChannel;
    import mx.messaging.events.ChannelEvent;
    
	public class channel extends EventDispatcher implements IVersiClass, IInitializeClass {
		private var _destination:String;
		private var _name:String;
		private var _endpoint:String;
		
		[Bindable]
		public var channelset:ChannelSet;
		
		//* Properties *//
		public function get name():String
		{
			return _name;
		}
		public function set name(setVal:String):void
		{
			_name=setVal;
		}
		
		public function get id():String
		{
			return _name;
		}
		public function set id(setVal:String):void
		{
			_name=setVal;
		}
		
				
		public function set endpoint(endpoint:String):void {
            _endpoint = endpoint;
        }

        public function get endpoint():String {
            return _endpoint;
        }
		
		
		public function set destination(destination:String):void {
			_destination = destination;
		}
		
		[Bindable]
		public function get destination():String {
			return _destination;
		}
		
			
		public function createChannel(force:Boolean=false):void
		{
			if (destination && endpoint && (this.channelset==null || force)) 
			{
				channelset = new ChannelSet();
				var channel:Channel = new AMFChannel(destination,endpoint)
				channelset.addChannel(channel);
				channelset.addEventListener(ChannelEvent.DISCONNECT,connectionFailure)
				this.dispatchEvent(new Event(ChannelEvent.CONNECT));
			}
		}
		
		public function invalidateProperties():void
		{
			if (channelset && channelset.connected)
			{
				channelset.removeEventListener(ChannelEvent.DISCONNECT,connectionFailure)
				channelset.disconnectAll();
				channelset = null;
				this.dispatchEvent(new Event(ChannelEvent.DISCONNECT));
			}
			else if (channelset)
			{
				channelset.removeEventListener(ChannelEvent.DISCONNECT,connectionFailure)
				channelset=null;	
			}
			createChannel()
		}
		
		public function connectionFailure(evt:ChannelEvent):void
		{
			channelset.removeEventListener(ChannelEvent.DISCONNECT,connectionFailure)
			channelset.disconnectAll();
			this.dispatchEvent(new Event(ChannelEvent.DISCONNECT));
			createChannel(true);
		}
		
		public function disconnect():void
		{
			if (this.channelset)
			{
				this.channelset.removeEventListener(ChannelEvent.DISCONNECT,connectionFailure)
				this.channelset.disconnectAll();
				this.channelset = null;
			}
			this.dispatchEvent(new Event(ChannelEvent.DISCONNECT));
		}
		
		public function onInitializeClass():void
		{
			createChannel()
		}
		
		//* Constructor *//
		public function channel()
		{
			super()
		}




	}

}